package vn.com.atm;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.com.atm.database.UserFunctions;
import vn.com.atm.map.CustomItemizedOverlay;
import vn.com.atm.map.CustomOverlayItem;
import vn.com.atm.obj.RouteObj;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.maps.Overlay;

public class AtmDetail extends Activity{
	UserFunctions us;
	JSONObject js;
	TextView tv1,tvDistance, tvTime;
	Button btnViewMap ;
	ListView lv;
	ArrayList<RouteObj> arrayregister;
	AdapterRegister adap;
	int[] images = {R.drawable.left,R.drawable.right, R.drawable.slight_left, R.drawable.slight_right, R.drawable.round, R.drawable.contine, R.drawable.flag};
	Drawable drawable, drawable2;
	CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay, itemizedOverlay1;
	List<Overlay> mapOverlays;
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.atmdetail);
		
	}
	   
	    @Override
	    protected void onStop() {
	    	
	    	super.onStop();
	    	
	    }
	    @Override
	    protected void onResume() {
	    	adap.notifyDataSetChanged();
	    	super.onResume();
	    }
	    @Override
	    protected void onDestroy() {
	    	super.onDestroy();
	    	adap.clear();
	    }
	protected String getMyLat() {
	    us = new UserFunctions();
	    js = new JSONObject();
		js = us.getMylocation();
		String str = "";
		try {
			str = js.getString("latitude");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return str;
		
	}
	protected String getMyLong() {
	
		String str = "";
		try {
			str = js.getString("longitude");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return str;
		
	}
	private Bitmap getBitMap(String bm)
	{
		if(bm.contains("Turn left")== true)
		{
			return BitmapFactory.decodeResource(getResources(), images[0]);
		}
		else if(bm.contains("Turn right")== true)
		{
			return BitmapFactory.decodeResource(getResources(), images[1]);
		}
		else if(bm.contains("Slight left")== true || bm.contains("Sharp left")== true)
		{
			return BitmapFactory.decodeResource(getResources(), images[2]);
		}
		else if(bm.contains("Slight right")== true)
		{
			return BitmapFactory.decodeResource(getResources(), images[3]);
		}
		else if(bm.contains("roundabout")== true)
		{
			return BitmapFactory.decodeResource(getResources(), images[4]);
		}
		else 
		{
			return BitmapFactory.decodeResource(getResources(), images[5]);
		}
	}
	private String RemoveHtml(String strSrc)
	{
		String s = Html.fromHtml(strSrc).toString().replace("\n", " ");
		return s;
	}
	private String ConvertVietNam(String strSrc)
	{
		
		
		strSrc = strSrc.replace("Slight left", "Chếch sang trái");
		strSrc = strSrc.replace("Slight right", "Chếch sang phải");
		strSrc = strSrc.replace("Sharp left", "Rẽ sang trái");
		strSrc = strSrc.replace("Head north on", "Đi về hướng bắc");
		strSrc = strSrc.replace("Pass by", "Đi qua");
		strSrc = strSrc.replace("on the right", "bên phải");
		strSrc = strSrc.replace("Turn right", "Rẽ phải");
		strSrc = strSrc.replace("Turn left", "Rẽ trái");
		strSrc = strSrc.replace("Continue", "tiếp tục");
		strSrc = strSrc.replace("continue", "tiếp tục");
		strSrc = strSrc.replace("take the 1st exit", "lối ra 1");
		strSrc = strSrc.replace("to stay on", "để vào");
		strSrc = strSrc.replace("toward", "hướng");
		strSrc = strSrc.replace("on the left", "bên trái");
		strSrc = strSrc.replace("the roundabout", "Đường vòng");
		strSrc = strSrc.replace("At ", "ở ");
		strSrc = strSrc.replace(" at ", " ở ");
		strSrc = strSrc.replace(" in ", " ở ");
		strSrc = strSrc.replace("onto", "vào");
		
		return strSrc;
	}
	@Override
	protected void onStart() {
		super.onStart();
	     arrayregister = new ArrayList<RouteObj>(); 
		 adap = new AdapterRegister(this,R.layout.list_route, arrayregister);
		 
		 // Get latsource, longsource, latdes, longdes
		Bundle bundle = getIntent().getExtras();
		double strLatDes = Double.parseDouble(bundle.getString("Lat"));
		double strLongDes = Double.parseDouble(bundle.getString("Long"));
		double strLatSrc = Double.parseDouble(this.getMyLat());
		double strLongSrc = Double.parseDouble(this.getMyLong());
		tvDistance = (TextView)findViewById(R.id.tvDis);
		tvTime = (TextView)findViewById(R.id.tvDuration);
		
		
		// Xu ly json file
		us = new UserFunctions();
		js = new JSONObject();
		js = us.getPoints(strLatSrc, strLongSrc, strLatDes, strLongDes);
     	JSONArray routeArray = null, legsArray = null, stepsArray = null;
     	JSONObject routes = null, legsObj = null;
     	String tvItrodu = "";
     	Bitmap bm = null;
     	try {
			routeArray = js.getJSONArray("routes");
			routes = routeArray.getJSONObject(0);
			legsArray = routes.getJSONArray("legs");
			legsObj = legsArray.getJSONObject(0);
			stepsArray = legsObj.getJSONArray("steps");
			tvDistance.setText("Khoảng cách: "+ legsObj.getJSONObject("distance").getString("text"));
			tvTime.setText("Thời gian: "+legsObj.getJSONObject("duration").getString("text"));
			String tvXP = legsObj.getString("start_address");
			RouteObj objx = new RouteObj(tvXP, BitmapFactory.decodeResource(getResources(), images[6]));
			arrayregister.add(objx);
			for(int i = 0; i< stepsArray.length(); i++)
			{
				tvItrodu = stepsArray.getJSONObject(i).getString("html_instructions");
				tvItrodu = this.RemoveHtml(tvItrodu);
				bm = this.getBitMap(tvItrodu);
				tvItrodu = this.ConvertVietNam(tvItrodu);
				RouteObj obj = new RouteObj(tvItrodu, bm);
				arrayregister.add(obj);
			}
			
			String dich = legsObj.getString("end_address");
			RouteObj objd = new RouteObj(dich, BitmapFactory.decodeResource(getResources(), images[6]));
			arrayregister.add(objd);
			
			lv =(ListView)findViewById(R.id.listview1);
     		lv.setAdapter(adap);
     		// ve duong
		} catch (JSONException e) {
			e.printStackTrace();
		}

     	
         
        	}
	 public class AdapterRegister extends ArrayAdapter<RouteObj>{
	    	Context mcontext;
	    	int rource;
	    	ArrayList<RouteObj> array;
	    	
	    	public AdapterRegister(Context context, int textViewResourceId,	ArrayList<RouteObj> arrays) {
				super(context, textViewResourceId, arrays);
				
				this.mcontext = context;
				this.array = arrays;
				this.rource = textViewResourceId;
			}

	    	@Override
	    	public View getView(int position, View convertView, ViewGroup parent) {
	    		View view = convertView;
	    		RouteObj item;
	    		
	    		if(view==null){
	    			view = getLayoutInflater().inflate(R.layout.list_route, null);
	    		}
	    		if((item=this.array.get(position))==null){
	    			return view;
	    		}
	    		
	    		ImageView icon = (ImageView)view.findViewById(R.id.list_image1);
	    		TextView itro = (TextView)view.findViewById(R.id.Introduration);
	    	
	    		
	    		
	    		icon.setImageBitmap(item.bm);
	    		itro.setText(item.tv);
	    		
	    		
	    		return view;
	    	}

	    }
}
